<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/30
 * Time: 10:20
 */
namespace app\wxapi\controller;

use think\Db;
use think\Request;
use think\Controller;
class Index extends Common
{
    /*
     * 核销景点主页面
     */
    public function tour_index()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $type = input('type','1','intval');
            //获取今日的数据
            $beginToday=date('Y-m-d 00:00:00', time());

            $endToday=date('Y-m-d 23:59:59', time());
            $where['create_time'] = ['between time', [$beginToday,$endToday]];
            //获取本月的数据
            $beginThisMonth = date('Y-m-01 00:00:00');
            $endThisMonth = date('Y-m-d 23:59:59', strtotime('Last day of this month'));
            $where1['create_time'] = ['between time',[$beginThisMonth,$endThisMonth]];
            //获取本年数据
            $beginThisYear = date('Y-01-01 00:00:00');
            $endThisYear = date('Y-12-31 23:59:59');
            $where2['create_time'] = ['between time',[$beginThisYear,$endThisYear]];

            //查询是哪个商户的
            $data = Db::name('tours')->field('tour_name as store_name')->where('tour_id',$post['id'])->find();
            //获取商户用户

            $name= Db::name('tour_admin')->field('admin_rename')->where('t_admin_id',$post['admin_id'])->find();
            if($type==1){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where)
                    ->where('cancel_id',$post['admin_id'])->where('type',1)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where)
                    ->where('cancel_id',$post['admin_id'])->where('type',1)->sum('money');
            }
            if($type==2){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where1)
                    ->where('cancel_id',$post['admin_id'])->where('type',1)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where1)
                    ->where('cancel_id',$post['admin_id'])->where('type',1)->sum('money');
            }
            if($type==3){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where2)
                    ->where('cancel_id',$post['admin_id'])->where('type',1)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where2)
                    ->where('cancel_id',$post['admin_id'])->where('type',1)->sum('money');
            }

            $data['order_list'] = Db::name('order_cancel')->field('order_sn,create_time')->where($where)
                ->where('cancel_id',$post['admin_id'])->where('type',1)->select();

            $data = array_merge($name,$data);
            $this->apiReturn('200','成功',$data);
        }
    }

    /*
     * 核销酒店主页面
     */
    public function hotel_index()
    {
        if(Request::instance()->isPost()){
            $post =  Request::instance()->post();
            $type = input('type','1','intval');
            //获取今日的数据
            $beginToday=date('Y-m-d 00:00:00', time());

            $endToday=date('Y-m-d 23:59:59', time());
            $where['create_time'] = ['between time', [$beginToday,$endToday]];
            //获取本月的数据
            $beginThisMonth = date('Y-m-01 00:00:00');
            $endThisMonth = date('Y-m-d 23:59:59', strtotime('Last day of this month'));
            $where1['create_time'] = ['between time',[$beginThisMonth,$endThisMonth]];
            //获取本年数据
            $beginThisYear = date('Y-01-01 00:00:00');
            $endThisYear = date('Y-12-31 23:59:59');
            $where2['create_time'] = ['between time',[$beginThisYear,$endThisYear]];

            //查询是哪个商户的
            $data = Db::name('hotel')->field('hotel_name as store_name')->where('hotel_id',$post['id'])->find();
            //获取商户用户

            $name= Db::name('hotel_admin')->field('admin_rename')->where('h_admin_id',$post['admin_id'])->find();
            if($type==1){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where)
                    ->where('cancel_id',$post['admin_id'])->where('type',2)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where)
                    ->where('cancel_id',$post['admin_id'])->where('type',2)->sum('money');
            }
            if($type==2){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where1)
                    ->where('cancel_id',$post['admin_id'])->where('type',2)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where1)
                    ->where('cancel_id',$post['admin_id'])->where('type',2)->sum('money');
            }
            if($type==3){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where2)
                    ->where('cancel_id',$post['admin_id'])->where('type',2)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where2)
                    ->where('cancel_id',$post['admin_id'])->where('type',2)->sum('money');
            }

            $data['order_list'] = Db::name('order_cancel')->field('order_sn,create_time')->where($where)
                ->where('cancel_id',$post['admin_id'])->where('type',2)->select();

            $data = array_merge($name,$data);
            $this->apiReturn('200','成功',$data);
        }
    }

    /*
     * 旅行社核销主页
     */
    public function team_index()
    {
        if(Request::instance()->isPost()){
            $post =  Request::instance()->post();
            $type = input('type','1','intval');
            //获取今日的数据
            $beginToday=date('Y-m-d 00:00:00', time());

            $endToday=date('Y-m-d 23:59:59', time());
            $where['create_time'] = ['between time', [$beginToday,$endToday]];
            //获取本月的数据
            $beginThisMonth = date('Y-m-01 00:00:00');
            $endThisMonth = date('Y-m-d 23:59:59', strtotime('Last day of this month'));
            $where1['create_time'] = ['between time',[$beginThisMonth,$endThisMonth]];
            //获取本年数据
            $beginThisYear = date('Y-01-01 00:00:00');
            $endThisYear = date('Y-12-31 23:59:59');
            $where2['create_time'] = ['between time',[$beginThisYear,$endThisYear]];

            //查询是哪个商户的
            $data = Db::name('team')->field('team_name as store_name')->where('team_id',$post['id'])->find();
            //获取商户用户

            $name= Db::name('team_admin')->field('admin_rename')->where('t_admin_id',$post['admin_id'])->find();
            if($type==1){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where)
                    ->where('cancel_id',$post['admin_id'])->where('type',3)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where)
                    ->where('cancel_id',$post['admin_id'])->where('type',3)->sum('money');
            }
            if($type==2){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where1)
                    ->where('cancel_id',$post['admin_id'])->where('type',3)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where1)
                    ->where('cancel_id',$post['admin_id'])->where('type',3)->sum('money');
            }
            if($type==3){
                $data['order_count'] = Db::name('order_cancel')
                    ->where($where2)
                    ->where('cancel_id',$post['admin_id'])->where('type',3)->count();
                $data['order_money'] = Db::name('order_cancel')->where($where2)
                    ->where('cancel_id',$post['admin_id'])->where('type',3)->sum('money');
            }

            $data['order_list'] = Db::name('order_cancel')->field('order_sn,create_time')->where($where)
                ->where('cancel_id',$post['admin_id'])->where('type',3)->select();

            $data = array_merge($name,$data);
            $this->apiReturn('200','成功',$data);
        }
    }

    /*
     * 景点核销记录
     */
    public function tour_cancel_list()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $start = strtotime($post['time']);
            $beginToday=date('Y-m-d 00:00:00', $start);

            $endToday=date('Y-m-d 23:59:59', $start);
            $where['create_time'] = ['between time', [$beginToday,$endToday]];
            $data = Db::name('order_cancel')->field('order_sn,create_time')->where($where)
                ->where('cancel_id',$post['admin_id'])->where('type',1)->select();
            $this->apiReturn('200','成功',$data);

        }
    }

    /*
     * 酒店核销列表
     */
    public function hotel_cancel_list()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $start = strtotime($post['time']);
            $beginToday=date('Y-m-d 00:00:00', $start);

            $endToday=date('Y-m-d 23:59:59', $start);
            $where['create_time'] = ['between time', [$beginToday,$endToday]];
            $data = Db::name('order_cancel')->field('order_sn,create_time')->where($where)
                ->where('cancel_id',$post['admin_id'])->where('type',2)->select();
            $this->apiReturn('200','成功',$data);

        }
    }
    /*
     * 旅行社核销
     */
    public function team_cancel_list()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $start = strtotime($post['time']);
            $beginToday=date('Y-m-d 00:00:00', $start);

            $endToday=date('Y-m-d 23:59:59', $start);
            $where['create_time'] = ['between time', [$beginToday,$endToday]];
            $data = Db::name('order_cancel')->field('order_sn,create_time')->where($where)
                ->where('cancel_id',$post['admin_id'])->where('type',3)->select();
            $this->apiReturn('200','成功',$data);
        }
    }
}